
//
//  FounctionBottomView.swift
//  Wmsmobile
//
//  Created by Nick xu on 16/8/8.
//  Copyright © 2016年 hddden. All rights reserved.
//

import UIKit

protocol FounctionBottomViewDelegate:NSObjectProtocol {
    func clickButton(_ button: UIButton)
}

class FounctionBottomView: UIView {
    //MARK: - property
    weak var delegate:FounctionBottomViewDelegate?
    var heightSelf: CGFloat?
    var heightMax: CGFloat?
    var array : Array<Dictionary<String , String>>?
    
    convenience init(array: Array<Dictionary<String , String>>){
        self.init()
        self.array = array
        self.backgroundColor = UIColor.hexStringToUIColor("#CCCCCC")
        self.frame = CGRect(x: 0, y: KscreenHeight - FBVButtonHeight - FBVButtonSpace - 64, width: KscreenWidth, height: FBVButtonHeight + FBVButtonSpace)
        self.heightSelf = FBVButtonHeight + FBVButtonSpace
        let number = array.count
        for i in 0..<number {
            if i < array.count{
                self.addChildButton(array[i]["title"]!, order: i, tag: 100 + i)
        }
    }
    }
    //MARK: - private
    fileprivate func addChildButton(_ title:String, order:NSInteger , tag: NSInteger){
        let button = UIButton()
        button.tag = tag
        button.setTitle(title, for: UIControlState())
        button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        button.setTitleColor(UIColor.hexStringToUIColor("#333333"), for: UIControlState())
        button.setBackgroundImage(UIImage(named: "e5e5e5"), for: UIControlState())
        button.setBackgroundImage(UIImage(named: "CCCCCC"), for: UIControlState.highlighted)
        self.addSubview(button)
        let magin:CGFloat = FBVButtonMagin
        let space:CGFloat = FBVButtonSpace
        let column = 4
        let width = (KscreenWidth - CGFloat((self.array?.count)!) + 1) / CGFloat((self.array?.count)!)
        let height:CGFloat = FBVButtonHeight
        let col = order % column
        let row = order / column
        let x = (magin + width) * CGFloat(col) + space
        let y = (magin + height) * CGFloat(row) + space
        button.frame = CGRect(x: x, y: y, width: width, height: height)
        button.addTarget(self, action: #selector(FounctionBottomView.clickButton(_:)), for: UIControlEvents.touchUpInside)
    }
//     //MARK: - private
    func clickButton(_ button: UIButton){
        if self.delegate != nil {
            self.delegate?.clickButton(button)
        }
    }
    

}
